package com.tmpt.controller.taglib.generic;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.tmpt.dto.PaginationDto;

public class Pagination extends BodyTagSupport{
	private static final long serialVersionUID = 1L;
	private String paginationDtoId;
	
	public Object execute(){
		try{
			PaginationDto paginationDto = (PaginationDto)pageContext.getAttribute(paginationDtoId);
			if(paginationDto==null){
				return null;
			}
			
			StringBuilder sb = new StringBuilder();
			//javascript
			sb.append("<script type='text/javascript'>").append("\r\n");
			sb.append("function resetPageNo(){").append("\r\n");
			sb.append("document.paginationForm.pageNo.options.length=0;").append("\r\n");
			sb.append("var option = new Option('1',pageNo);").append("\r\n");
			sb.append("document.paginationForm.pageNo.options.add(option);").append("\r\n");
			sb.append("}").append("\r\n");
			sb.append("function paginationSubmit(pageNo){").append("\r\n");
			sb.append("this.disabled=true;").append("\r\n");
			sb.append("if(pageNo==null){").append("\r\n");
			sb.append("document.paginationForm.submit();").append("\r\n");
			sb.append("return;").append("\r\n");
			sb.append("}").append("\r\n");
			sb.append("document.paginationForm.pageNo.options.length=0;").append("\r\n");
			sb.append("var option = new Option(pageNo,pageNo);").append("\r\n");
			sb.append("document.paginationForm.pageNo.options.add(option);").append("\r\n");
			sb.append("document.paginationForm.submit();").append("\r\n");
			sb.append("}").append("\r\n");
			sb.append("</script>").append("\r\n");
			
			//html
			//sb.append("<table  border='0' cellspacing='0' cellpadding='4' bgcolor='#F4F7F8' bordercolor='#D3D3D3'>").append("\r\n");
			sb.append("<table  border='0' cellspacing='0' cellpadding='4' >").append("\r\n");
			sb.append("<tr>").append("\r\n");
			sb.append("<td colspan='100' align='right'>").append("\r\n");
			sb.append("共"+paginationDto.getTotalRecordCount()+"条数据").append("\r\n");
			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;").append("\r\n");
			sb.append("第"+paginationDto.getPageNo()+"页/共"+paginationDto.getTotalPageCount()+"页").append("\r\n");
			sb.append("&nbsp;&nbsp;&nbsp;").append("\r\n");
			
			if(paginationDto.getPageNo()>1){
				sb.append("[<a onclick='javascript:paginationSubmit(1);' style='color: red;cursor: pointer;'>首页</a>]").append("\r\n");
			}else{
				sb.append("[首页]").append("\r\n");
			}
			sb.append("&nbsp;").append("\r\n");
			
			if(paginationDto.isHasPreviousPage()==true){
				sb.append("[<a onclick='javascript:paginationSubmit("+paginationDto.getPreviousPageNo()+");' style='color: red;cursor: pointer;' >上一页</a>]").append("\r\n");
			}else{
				sb.append("[上一页]").append("\r\n");
			}
			sb.append("&nbsp;").append("\r\n");
			
			if(paginationDto.isHasNextPage()==true){
				sb.append("[<a onclick='javascript:paginationSubmit("+paginationDto.getNextPageNo()+");' style='color: red;cursor: pointer;'>下一页</a>]").append("\r\n");
			}else{
				sb.append("[下一页]").append("\r\n");	
			}
			sb.append("&nbsp;");

			if(paginationDto.isHasEndPage()==true){
				sb.append("[<a onclick='javascript:paginationSubmit("+paginationDto.getEndPageNo()+");' style='color: red;cursor: pointer;'>末页</a>]").append("\r\n");
			}else{
				sb.append("[末页]").append("\r\n");	
			}
			sb.append("&nbsp;&nbsp;&nbsp;&nbsp;").append("\r\n");
			
			sb.append("第").append("\r\n");
			sb.append("<select name='pageNo' onchange='javascript:paginationSubmit();'>").append("\r\n");
			for(int i=0;i<paginationDto.getPageList().size();i++){
				Object pageNoTmp = paginationDto.getPageList().get(i);
				sb.append("<option id='pageNo_").append(pageNoTmp).append("'>").append("\r\n");
				sb.append(pageNoTmp).append("\r\n");
				sb.append("</option>").append("\r\n");
			}
			sb.append("</select>").append("\r\n");
			sb.append("页").append("\r\n");
			
			sb.append("&nbsp;&nbsp;").append("\r\n");
			
			sb.append("每页显示记录数: ").append("\r\n");
			sb.append("<select name='pageSize' onchange='javascript:paginationSubmit(1);'>").append("\r\n");
			for(int i=0;i<paginationDto.getPageSizeList().size();i++){
				Object pageSizeTmp = paginationDto.getPageSizeList().get(i);
				sb.append("<option id='pageSize_").append(pageSizeTmp).append("'>").append("\r\n");
				sb.append(pageSizeTmp).append("\r\n");
				sb.append("</option>").append("\r\n");
			}
			sb.append("</select>").append("\r\n");
			

			sb.append("<script type='text/javascript'>").append("\r\n");
			sb.append("document.getElementById('pageSize_").append(paginationDto.getPageSize()).append("').selected='selected'").append("\r\n");
			sb.append("document.getElementById('pageNo_").append(paginationDto.getPageNo()).append("').selected='selected'").append("\r\n");
			sb.append("</script>").append("\r\n");
			
			sb.append("</td>").append("\r\n");
			sb.append("</tr>").append("\r\n");
			sb.append("</table>").append("\r\n");
			
			JspWriter out = pageContext.getOut();
			out.println(sb.toString());
		}catch(Exception e){
			e.printStackTrace();
		}

		return null;
	}
	
	@Override
	public int doStartTag() throws JspException {
		this.execute();
		return super.doStartTag();
	}

	public int doEndTag() throws JspException {		
		return super.doEndTag();
	}

	public void release() {
		super.release();
		id = null;
	}

	public String getPaginationDtoId() {
		return paginationDtoId;
	}

	public void setPaginationDtoId(String paginationDtoId) {
		this.paginationDtoId = paginationDtoId;
	}

	
}
